package org.darkforest.admin.interceptor;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.darkforest.admin.service.AdminLogService;
import org.darkforest.annotation.ArchivesLog;
import org.darkforest.vo.MemberBase;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * 日志处理类
 *
 * @auth bingo
 * @create 2017-11-17-11:27
 */
@Component
public class ArchivesLogAspect {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(ArchivesLogAspect.class);


    private Long createtime = 0L;

    private String url = "";
    private HttpServletRequest request = null;

    private MemberBase memberBase = null;


    @Autowired
    private AdminLogService adminLogService;


    /**
     *
     * @Description: 方法调用前触发   记录开始时间
     * @author bingo
     * @date 2017年11月17日
     * @param joinPoint
     */
    public void before(JoinPoint joinPoint){
        request = getHttpServletRequest();
        memberBase = (MemberBase)request.getSession().getAttribute("memInfo");
        createtime = System.currentTimeMillis(); //记录方法开始执行的时间

    }




    /**
     *
     * @Description: 方法调用后触发   记录结束时间
     * @author fei.lei
     * @date 2016年11月23日 下午5:10
     * @param joinPoint
     */
    public  void after(JoinPoint joinPoint) {
        request = getHttpServletRequest();
        url = request.getRequestURL().toString();
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        ip = ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;

        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = null;
        try {
            targetClass = Class.forName(targetName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Method[] methods = targetClass.getMethods();
        String operationName = "";
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs!=null&&clazzs.length == arguments.length&&method.getAnnotation(ArchivesLog.class)!=null) {
                    operationName = method.getAnnotation(ArchivesLog.class).operationDescription();
                    logger.debug(" 操作人: " + memberBase.getUserName() + " 操作方法: " + operationName + " 操作开始时间: " + createtime);
                    adminLogService.addAdminLogs(memberBase.getUserId(),memberBase.getUserName(),ip,url,operationName);
                    break;
                }
            }
        }

    }

    /**
     *
     * @Title：around
     * @Description: 环绕触发
     * @author fei.lei
     * @date 2016年11月23日 下午5:10
     * @param joinPoint
     * @return Object
     * @throws Throwable
     */
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        return null;
    }


    /**
     * @Description: 获取request
     * @param
     * @return HttpServletRequest
     */
    public HttpServletRequest getHttpServletRequest(){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)ra;
        HttpServletRequest request = sra.getRequest();
        return request;
    }
}
